package com.carey.blog.event.sync.impl;

import java.util.logging.Level;
import java.util.logging.Logger;

import org.json.JSONException;
import org.json.JSONObject;

import com.carey.blog.event.sync.AbstractAddArticleProcessor;
import com.carey.blog.event.sync.BlogSyncStatusCodes;
import com.carey.blog.framework.latke.Keys;
import com.carey.blog.framework.latke.event.Event;
import com.carey.blog.framework.latke.event.EventException;
import com.carey.blog.framework.latke.event.EventManager;
import com.carey.blog.model.BlogSync;
import com.carey.blog.sync.SyncException;

/**
 * This listener is responsible for blog sync add article to CnBlogs.
 * 
 */
public final class CnBlogsAddArticleProcessor extends
		AbstractAddArticleProcessor {

	/**
	 * Logger.
	 */
	private static final Logger LOGGER = Logger
			.getLogger(CnBlogsAddArticleProcessor.class.getName());

	/**
	 * Constructs a {@link CnBlogsAddArticleProcessor} object with the specified
	 * event manager.
	 * 
	 * @param eventManager
	 *            the specified event manager
	 */
	public CnBlogsAddArticleProcessor(final EventManager eventManager) {
		super(eventManager);
	}

	@Override
	public String getExternalBloggingSys() {
		return BlogSync.BLOG_SYNC_CNBLOGS;
	}

	@Override
	public void action(final Event<JSONObject> event) throws EventException {
		final JSONObject eventData = event.getData();
		JSONObject result = null;
		JSONObject events = null;
		JSONObject blogSyncCnBlogs = null;
		try {
			result = eventData.getJSONObject(Keys.RESULTS);
			final JSONObject status = result.getJSONObject(Keys.STATUS);

			events = status.optJSONObject(Keys.EVENTS);
			if (null == events) {
				events = new JSONObject();
				status.put(Keys.EVENTS, events);
			}

			blogSyncCnBlogs = events.optJSONObject(BlogSync.BLOG_SYNC_CNBLOGS);
			if (null == blogSyncCnBlogs) {
				blogSyncCnBlogs = new JSONObject();
				events.put(BlogSync.BLOG_SYNC_CNBLOGS, blogSyncCnBlogs);
			}
		} catch (final JSONException e) {
			LOGGER.log(Level.SEVERE, e.getMessage(), e);
			throw new EventException(e);
		}

		try {
			final JSONObject rslt = addArticle(event);
			try {
				blogSyncCnBlogs
						.put(Keys.CODE, rslt.getString(Keys.STATUS_CODE));
			} catch (final JSONException ex) {
				LOGGER.log(Level.SEVERE, ex.getMessage(), ex);
				throw new EventException(ex);
			}
		} catch (final SyncException e) {
			try {
				blogSyncCnBlogs.put(Keys.CODE,
						BlogSyncStatusCodes.BLOG_SYNC_FAIL);
				blogSyncCnBlogs.put(Keys.MSG, e.getMessage());
			} catch (final JSONException ex) {
				LOGGER.log(Level.SEVERE, ex.getMessage(), ex);
				throw new EventException(ex);
			}
		} catch (final EventException e) {
			LOGGER.log(Level.SEVERE, e.getMessage(), e);
			try {
				blogSyncCnBlogs.put(Keys.CODE,
						BlogSyncStatusCodes.BLOG_SYNC_FAIL);
				blogSyncCnBlogs.put(Keys.MSG, "Unknown exception :-(");
			} catch (final JSONException ex) {
				LOGGER.log(Level.SEVERE, ex.getMessage(), ex);
				throw new EventException(ex);
			}
		}
	}
}
